package com.share.device.controller;

import com.share.common.core.web.domain.AjaxResult;
import com.share.common.security.annotation.RequiresPermissions;
import com.share.device.domain.Staff;
import com.share.device.domain.WorkOrder;
import com.share.device.domain.dto.WorkOrderDto;
import com.share.device.domain.vo.WorkOrderVo;
import com.share.device.service.StaffService;
import com.share.device.service.WorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/work")
public class WorkOrderController {
    @Autowired
    private WorkOrderService workOrderService;
    @Autowired
    private StaffService staffService;
    //工单列表
    @RequiresPermissions("device:work:workOrderList")
    @GetMapping("/workOrderList")
    public AjaxResult workOrderList(WorkOrderDto workOrderDto){
       return AjaxResult.success(workOrderService.workOrderList(workOrderDto));
    }

    // //维护人员下拉框
    @GetMapping("/getStaffList")
    public AjaxResult getStaffList(){
        List<Staff> staffList = staffService.list();
        return AjaxResult.success(staffList);
    }

    //添加工单
    @PostMapping("/addWorkOrder")
    public AjaxResult addWorkOrder(@RequestBody WorkOrder workOrder){
        return AjaxResult.success(workOrderService.save(workOrder));
    }

    //工单回显
    @GetMapping("/getWorkOrderInfo/{id}")
    public AjaxResult getWorkOrderInfo(@PathVariable Integer id){
        WorkOrder workOrder = workOrderService.getById(id);
        return AjaxResult.success(workOrder);
    }

    //修改
    @PutMapping("/updateWorkOrder")
    public AjaxResult updateWorkOrder(@RequestBody WorkOrder workOrder){
        boolean update = workOrderService.updateById(workOrder);
        if(update){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //删除工单
    @DeleteMapping("/deleteWorkOrder/{id}")
    public AjaxResult deleteWorkOrder(@PathVariable Integer id){
        boolean delete = workOrderService.removeById(id);
        if(delete){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //查看工单详情
    @GetMapping("/getWorkOrderDetail/{id}")
    public AjaxResult getWorkOrderDetail(@PathVariable Integer id){
        WorkOrderVo workOrderVo = workOrderService.getWorkOrderDetail(id);
        return AjaxResult.success(workOrderVo);
    }



}
